package zqservlet;


import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebFilter("/*")
public class loginFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //--强制转换
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String ctxpath = request.getContextPath();

        StringBuffer url = request.getRequestURL();
        String ls_val = url.toString();
        String ls_one = ls_val.substring(0, ls_val.lastIndexOf("/")) + "/login";

        //--获取资源请求路径
        String uri = request.getRequestURI();

        //--判断是否包含登录相关路径
        if (uri.contains("/login") ||
                (uri.contains("/w_login.jsp")) ||
                (uri.contains("/zu")) ||
                (uri.contains("/gz")) ||
                (uri.contains("/hy")) ||
                (uri.contains("/dofive")) ||
                (uri.contains("/compos")) ||
                (uri.contains("/test")) ||
                (uri.contains("/img")) ||
                (uri.contains("/static")) ||
                (uri.contains("/lib"))) {
            chain.doFilter(req, resp);
        } else {
            //取出session中的用户信息，如果进行拦截判断
            String sessionOpid = (String) request.getSession().getAttribute("sessionOpid");
            String sessionOpmm = (String) request.getSession().getAttribute("sessionOpmm");
            if (sessionOpid != null) {
                if ("36;01431".equals(sessionOpmm) && !(uri.contains("/w_zxkj_chgpass.jsp"))&& !(uri.contains("/com2"))) {
                    String ls_html =
                            "<html>\n" +
                                    "<head>\n" +
                                    "    <meta charset=\"utf-8\">\n" +
                                    "    <meta name=\"renderer\" content=\"webkit|ie-comp|ie-stand\">\n" +
                                    "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n" +
                                    "    <meta name=\"viewport\" content=\"width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no\"/>\n" +
                                    "    <!--[if lt IE 9]>\n" +
                                    "    <script type=\"text/javascript\" src=\"lib/html5shiv.js\"></script>\n" +
                                    "    <script type=\"text/javascript\" src=\"lib/respond.min.js\"></script>\n" +
                                    "    <![endif]-->\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"static/h-ui/css/H-ui.min.css\"/>\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"static/h-ui.admin/css/H-ui.admin.css\"/>\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"lib/Hui-iconfont/1.0.8/iconfont.css\"/>\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"static/h-ui.admin/skin/default/skin.css\" id=\"skin\"/>\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"static/h-ui.admin/css/style.css\"/>\n" +
                                    "    <!--[if IE 6]>\n" +
                                    "</head>\n" +
                                    "<body>\n" +
                                    "<!--_footer 作为公共模版分离出去-->\n" +
                                    "<script type=\"text/javascript\" src=\"lib/jquery/1.9.1/jquery.min.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"lib/layer/2.4/layer.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"static/h-ui/js/H-ui.min.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"static/h-ui.admin/js/H-ui.admin.js\"></script> <!--/_footer 作为公共模版分离出去-->\n" +
                                    "\n" +
                                    "<!--请在下方写此页面业务相关的脚本-->\n" +
                                    "<script type=\"text/javascript\" src=\"lib/jquery.validation/1.14.0/jquery.validate.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"lib/jquery.validation/1.14.0/validate-methods.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"lib/jquery.validation/1.14.0/messages_zh.js\"></script>\n" +
                                    "<script type=\"text/javascript\">" +
                                    "layer.msg('请修改密码，然后进行相关操作！', {icon: 0, time: 3000});" +
                                    "</script>\n" +
                                    "</body>\n" +
                                    "</html>\n";
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write(ls_html);
                } else {
                    chain.doFilter(req, resp);
                }
            } else {
                //ajax session 过期处理，此处的判定以后，还需要在页面进行ajax完成后的处理，请查看页面代码，每个页面都有一个ajax完成的判断
                //1:判断是否是ajax请求
                if (request.getHeader("x-requested-with") != null && "XMLHttpRequest".equalsIgnoreCase(request.getHeader("x-requested-with"))) {
                    //向http头添加 状态 sessionstatus, session的超时时间设置在web.xml中，
                    response.setHeader("sessionstatus", "timeout");
                    response.setStatus(403);

                    //向http头添加登录的url
                    response.addHeader("loginPath", ctxpath);
                } else {
                    String ls_html =
                            "<html>\n" +
                                    "<head>\n" +
                                    "    <meta charset=\"utf-8\">\n" +
                                    "    <meta name=\"renderer\" content=\"webkit|ie-comp|ie-stand\">\n" +
                                    "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n" +
                                    "    <meta name=\"viewport\" content=\"width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no\"/>\n" +
                                    "    <!--[if lt IE 9]>\n" +
                                    "    <script type=\"text/javascript\" src=\"lib/html5shiv.js\"></script>\n" +
                                    "    <script type=\"text/javascript\" src=\"lib/respond.min.js\"></script>\n" +
                                    "    <![endif]-->\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"static/h-ui/css/H-ui.min.css\"/>\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"static/h-ui.admin/css/H-ui.admin.css\"/>\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"lib/Hui-iconfont/1.0.8/iconfont.css\"/>\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"static/h-ui.admin/skin/default/skin.css\" id=\"skin\"/>\n" +
                                    "    <link rel=\"stylesheet\" type=\"text/css\" href=\"static/h-ui.admin/css/style.css\"/>\n" +
                                    "    <!--[if IE 6]>\n" +
                                    "</head>\n" +
                                    "<body>\n" +
                                    "<!--_footer 作为公共模版分离出去-->\n" +
                                    "<script type=\"text/javascript\" src=\"lib/jquery/1.9.1/jquery.min.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"lib/layer/2.4/layer.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"static/h-ui/js/H-ui.min.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"static/h-ui.admin/js/H-ui.admin.js\"></script> <!--/_footer 作为公共模版分离出去-->\n" +
                                    "\n" +
                                    "<!--请在下方写此页面业务相关的脚本-->\n" +
                                    "<script type=\"text/javascript\" src=\"lib/jquery.validation/1.14.0/jquery.validate.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"lib/jquery.validation/1.14.0/validate-methods.js\"></script>\n" +
                                    "<script type=\"text/javascript\" src=\"lib/jquery.validation/1.14.0/messages_zh.js\"></script>\n" +
                                    "<script type=\"text/javascript\">" +
                                    "layer.msg('系统会话超时，请重新登录！1111111', {icon: 0, time: 3000},function(){window.open('" + ls_one + "','_top')});" +
                                    "</script>\n" +
                                    "</body>\n" +
                                    "</html>\n";
                    response.setContentType("text/html;charset=utf-8");
                    response.getWriter().write(ls_html);
                }
            }
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
